%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               marginal_design2.m
%
% Authors:            Sergio Ascencio and Miguel Rueda
%
% Description:        Creates datasets with info for simulated probabilities.

% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [mean_X_PRI,mean_X_PAN,mean_X_PRD]=marginal_design2(Y,X,beta,var,range_var,pri_pan_prd,phats)

%Extended
% varsx={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'l_diff_PRI_PRD' 'l_diff_PAN_PRD' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
% 'l_d_RC_others_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'l_diff_PRI_PRD_sq' 'l_diff_PAN_PRD_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
% 'l_d_RC_others_l_margin' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_l_diff_PRI_PRD' 'l_d_RC_others_l_diff_PAN_PRD' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' ...
% 'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_l_diff_PRI_PRD' 'l_margin_l_diff_PAN_PRD' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' ...
% 'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_l_diff_PRI_PRD' 'lcasillas_l_diff_PAN_PRD' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
% 'school_ic_l_diff_PRI_PAN' 'school_ic_l_diff_PRI_PRD' 'school_ic_l_diff_PAN_PRD' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
% 'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' 'l_diff_PRI_PRD_lpop' 'l_diff_PRI_PRD_l_turnout' 'l_diff_PRI_PRD_local' 'l_diff_PRI_PRD_ldist_city' ...
% 'l_diff_PAN_PRD_lpop' 'l_diff_PAN_PRD_l_turnout' 'l_diff_PAN_PRD_local' 'l_diff_PAN_PRD_ldist_city' ...
% 'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
% 'l_turnout_local' 'l_turnout_ldist_city' ...
% 'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN' 'ldist_prd' 'gov_PRD'};


%[phats,~]=first_stage2(Y,X);

%Expanded specification2
Z_PRD=X(:,end-1:end);
Z_PAN=X(:,end-3:end-2);
Z_PRI=X(:,end-5:end-4);

X=X(:,1:11);

%H and then M
p_PRI=phats{1,1}(:,1:2); 
p_PAN=phats{1,2}(:,1:2);
p_PRD=phats{1,3}(:,1:2);

X_PRI=[X Z_PRI p_PAN p_PRD ones(size(X,1),1)];
X_PAN=[X Z_PAN p_PRI p_PRD ones(size(X,1),1)];
X_PRD=[X Z_PRD p_PRI p_PAN ones(size(X,1),1)];

[p_PAN,p_PRI,p_PRD]=predicted_probs2(beta,X_PRI,X_PAN,X_PRD);

mean_X=mean(X,1);
mean_X(:,1)=1;
mean_X(:,10)=1;

mean_Z_PAN=mean(Z_PAN,1);
mean_Z_PRI=mean(Z_PRI,1);
mean_Z_PRD=mean(Z_PRD,1);
mean_Z_PAN(:,2)=0;
mean_Z_PRI(:,2)=0;
mean_Z_PRD(:,2)=0;

mean_p_PAN=mean(p_PAN,1);
mean_p_PRI=mean(p_PRI,1);
mean_p_PRD=mean(p_PRD,1);

if var==17&&pri_pan_prd==3
    
    mean_p_PAN=[mean_p_PAN(:,3) mean_p_PAN(:,2)];
    mean_p_PRI=[mean_p_PRI(:,3) mean_p_PRI(:,2)];
    mean_p_PRD=[0.6 mean_p_PRD(:,2)];    

elseif  var==15&&pri_pan_prd==1
        
    mean_p_PAN=[mean_p_PAN(:,3) mean_p_PAN(:,2)];
    mean_p_PRI=[0.6 mean_p_PRI(:,2)];
    mean_p_PRD=[mean_p_PRD(:,3) mean_p_PRD(:,2)];
    
elseif  var==15&&pri_pan_prd==2
    
    mean_p_PAN=[0.6 mean_p_PAN(:,2)];
    mean_p_PRI=[mean_p_PRI(:,3) mean_p_PRI(:,2)];
    mean_p_PRD=[mean_p_PRD(:,3) mean_p_PRD(:,2)];

else
    
    mean_p_PAN=[mean_p_PAN(:,3) mean_p_PAN(:,2)];
    mean_p_PRI=[mean_p_PRI(:,3) mean_p_PRI(:,2)];
    mean_p_PRD=[mean_p_PRD(:,3) mean_p_PRD(:,2)];
    
end    

mean_X_PRI=[mean_X mean_Z_PRI mean_p_PAN mean_p_PRD 1];
mean_X_PAN=[mean_X mean_Z_PAN mean_p_PRI mean_p_PRD 1];
mean_X_PRD=[mean_X mean_Z_PRD mean_p_PRI mean_p_PAN 1];

mean_X_PRI=kron(mean_X_PRI,ones(length(range_var),1));
mean_X_PAN=kron(mean_X_PAN,ones(length(range_var),1));
mean_X_PRD=kron(mean_X_PRD,ones(length(range_var),1));

if pri_pan_prd==3
    mean_X_PRI(:,var)=range_var;
    mean_X_PAN(:,var)=range_var;
elseif pri_pan_prd==2
    mean_X_PRI(:,var)=range_var;
    mean_X_PRD(:,var)=range_var;    
else
    mean_X_PAN(:,var)=range_var;
    mean_X_PRD(:,var)=range_var;
end
    
end

